package com.smileus;

import java.util.Collections;
import java.util.List;


/**
 * 与具体ORM实现无关的分页参数及查询结果封装.
 * 
 * @param <T>
 *            Page中记录的类型.
 * @author ben
 */
public class Page<T> {
	// 公共变量
	public static final String ASC = "asc";

	public static final String DESC = "desc";

	public static final int MIN_PAGESIZE = 5;

	public static final int DEFUALT_PAGESIZE = 15;

	public static final int MAX_PAGESIZE = 200;

	// 分页参数
	protected int pageNo = 1;

	protected int pageSize = DEFUALT_PAGESIZE;

	protected String orderBy = null;

	protected String order = ASC;

	protected boolean autoCount = true;

	// 返回结果
	protected List<T> result = null;

	protected long totalCount = -1;

	// 构�?�函�?

	public Page() {
		super();
	}

	public Page(final int pageSize) {
		setPageSize(pageSize);
	}

	public Page(final int pageSize, final boolean autoCount) {
		setPageSize(pageSize);
		this.autoCount = autoCount;
	}

	// 查询参数函数

	/**
	 * 获得当前页的页号,序号�?1�?�?,默认�?1.
	 */
	public int getPageNo() {
		return pageNo;
	}

	/**
	 * 设置当前页的页号,序号�?1�?�?,低于1时自动调整为1.
	 */
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;

		if (pageNo < 1) {
			this.pageNo = 1;
		}
	}

	/**
	 * 获得每页的记录数�?,默认�?20.
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 设置每页的记录数�?,超出MIN_PAGESIZE与MAX_PAGESIZE范围时会自动调整.
	 */
	public void setPageSize(final int pageSize) {
		this.pageSize = pageSize;

		/*
		 * if (pageSize < MIN_PAGESIZE) { this.pageSize = MIN_PAGESIZE; } if
		 * (pageSize > MAX_PAGESIZE) { this.pageSize = MAX_PAGESIZE; }
		 */
	}

	/**
	 * 根据pageNo和pageSize计算当前页第�?条记录在总结果集中的位置,序号�?0�?�?.
	 */
	public int getFirst() {
		return ((pageNo - 1) * pageSize);
	}

	/**
	 * 根据pageNo和pageSize计算当前页第�?条记录在总结果集中的位置,序号�?0�?�?.
	 */
	public int getEnd() {
		return getFirst() + getPageSize();
	}

	/**
	 * 获得排序字段,无默认�??.多个排序字段时用','分隔,仅在Criterion查询时有�?.
	 */
	public String getOrderBy() {
		return orderBy;
	}

	/**
	 * 设置排序字段.多个排序字段时用','分隔.仅在Criterion查询时有�?.
	 */
	public void setOrderBy(final String orderBy) {
		this.orderBy = orderBy;
	}

	/**
	 * 获得排序方向,默认为asc,仅在Criterion查询时有�?.
	 * 
	 * @param order
	 *            可�?��?�为desc或asc,多个排序字段时用','分隔.
	 */
	public String getOrder() {
		return order;
	}

	/**
	 * 查询对象时是否自动另外执行count查询获取总记录数,默认为false,仅在Criterion查询时有�?.
	 */
	public boolean isAutoCount() {
		return autoCount;
	}

	/**
	 * 查询对象时是否自动另外执行count查询获取总记录数,仅在Criterion查询时有�?.
	 */
	public void setAutoCount(final boolean autoCount) {
		this.autoCount = autoCount;
	}

	// 查询结果函数

	/**
	 * 取得页内的记录列�?.
	 */
	public List<T> getResult() {
		if (result == null)
			return Collections.emptyList();
		return result;
	}

	public void setResult(final List<T> result) {
		this.result = result;
	}

	/**
	 * 取得总记录数,默认值为-1.
	 */
	public long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(final long totalCount) {
		System.out.println("====>totalCount = " + totalCount);
		this.totalCount = totalCount;
	}

	/**
	 * 根据pageSize与totalCount计算总页�?,默认值为-1.
	 */
	public int getTotalPages() {
		if (totalCount < 0)
			return -1;

		int count = (int) (totalCount / pageSize);
		if (totalCount % pageSize > 0) {
			count++;
		}
		return count;
	}

	/**
	 * 是否还有下一�?.
	 */
	public boolean isHasNext() {
		return (pageNo + 1 <= getTotalPages());
	}

	/**
	 * 取得下页的页�?,序号�?1�?�?.
	 */
	public int getNextPage() {
		if (isHasNext())
			return pageNo + 1;
		else
			return pageNo;
	}

	/**
	 * 是否还有上一�?.
	 */
	public boolean isHasPre() {
		return (pageNo - 1 >= 1);
	}

	/**
	 * 取得上页的页�?,序号�?1�?�?.
	 */
	public int getPrePage() {
		if (isHasPre())
			return pageNo - 1;
		else
			return pageNo;
	}
}
